﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Touch2Like.Model;
using Touch2Like.DAL;
using Touch2Like.SharedLibrary;
using System.Data;

namespace Touch2Like.Web.Product
{
    public partial class Cart : System.Web.UI.Page
    {
        string userid = "";
        ShoppingCartDAL cart = new Touch2Like.DAL.ShoppingCartDAL();

        protected void Page_Load(object sender, EventArgs e)
        {
            userid = WebUtility.UserGUID;
            if (WebUtility.UserGUID == "0")
            {
                if (WebUtility.TempUserID == 0)
                {
                    Touch2Like.DAL.UserDAL user = new Touch2Like.DAL.UserDAL();
                    //WebUtility.TempUserID = user.AddTempUser();
                }

                //userid = WebUtility.TempUserID;
                //Response.Redirect("/Customer/signin.aspx?next=" + Server.UrlEncode("/goods/Cart.aspx?act=" + Request["act"] + "&c=" + Request["c"] + "&g=" + Request["g"]));
            }

            // this.Master.CurrentPage = "NFCStore";

            if (!IsPostBack)
            {
                string ordernumber = Guid.NewGuid().ToString();
                DataTable dtCart = cart.GetNoPaid(userid);
                if (dtCart.Rows.Count > 0)
                {
                    ordernumber = dtCart.Rows[0]["OrderNumber"].ToString();
                }

                if (!string.IsNullOrEmpty(Request["act"]) && Request["act"] == "Add")
                {
                    if (!string.IsNullOrEmpty(Request["c"]) && !string.IsNullOrEmpty(Request["g"]))
                    {
                        DataRow[] drsCart = dtCart.Select("SKU=" + Convert.ToInt32(Utility.Encryption.Decrypt(Request["g"])));

                        ShoppingCartInfo cartInfo = new ShoppingCartInfo();

                        cartInfo.Quantity = Convert.ToInt32(Request["c"]);
                        cartInfo.SKU = Convert.ToInt32(Utility.Encryption.Decrypt(Request["g"]));
                        cartInfo.OrderNumber = ordernumber;
                        cartInfo.CartStatus = 0;
                        cartInfo.UserGUID = userid;

                        ProductDAL product = new ProductDAL();
                        ProductInfo productInfo = product.GetProductsBySKU(cartInfo.SKU.Value);
                        if (productInfo != null)
                        {
                            cartInfo.Price = productInfo.Price;
                            cartInfo.TotalPrice = cartInfo.Price.Value * cartInfo.Quantity.Value;
                            if (drsCart.Length == 0)
                            {
                                cart.Add(cartInfo);
                            }
                            else
                            {
                                cartInfo = cart.GetByID(Convert.ToInt32(drsCart[0]["ListID"]));
                                if (cartInfo != null)
                                {
                                    cartInfo.Quantity = Convert.ToInt32(Request["c"]) + Convert.ToInt32(drsCart[0]["Quantity"]);
                                    cartInfo.TotalPrice = cartInfo.Price.Value * cartInfo.Quantity.Value;

                                    cart.Update(cartInfo);
                                }
                            }

                            Response.Redirect("Cart.aspx");
                            return;
                        }
                    }
                }

                BindData();
            }
        }

        void BindData()
        {
            DataTable dtCart = cart.GetNoPaid(userid);


            Repeater_Cart.DataSource = dtCart;
            Repeater_Cart.DataBind();
        }

        protected void btn_Delete_Click(object sender, EventArgs e)
        {
            LinkButton btn = (LinkButton)sender;
            int listid = Convert.ToInt32(btn.CommandArgument);

            cart.Delete(listid);

            BindData();
        }

        protected void btn_Checkout_Click(object sender, EventArgs e)
        {

            if (Repeater_Cart.Items.Count <= 0)
            {
                PH_NoGoods.Visible = true;
                return;
            }

            OrderDAL order = new OrderDAL();
            OrderInfo orderInfo = new OrderInfo();
            ProductDAL product = new ProductDAL();

            DataTable dtCart = cart.GetByCartStatus(userid, 0);

            string ordernumber = Guid.NewGuid().ToString();
            decimal orderprice = 0;

            foreach (RepeaterItem item in Repeater_Cart.Items)
            {
                HiddenField tb_ListID = item.FindControl("tb_ListID") as HiddenField;
                HiddenField tb_OrderNumber = item.FindControl("tb_OrderNumber") as HiddenField;
                HiddenField tb_GoodsID = item.FindControl("tb_GoodsID") as HiddenField;
                TextBox tb_GoodsCount = item.FindControl("tb_GoodsCount") as TextBox;

                if (string.IsNullOrEmpty(tb_GoodsCount.Text)) tb_GoodsCount.Text = "0";

                ShoppingCartInfo cartInfo = cart.GetByID(Convert.ToInt32(tb_ListID.Value));
                if (cartInfo != null)
                {
                    ProductInfo productInfo = product.GetProductsBySKU(Convert.ToInt32(tb_GoodsID.Value));
                    if (productInfo != null)
                    {
                        cartInfo.Price = productInfo.Price;
                        cartInfo.Quantity = Convert.ToInt32(tb_GoodsCount.Text);
                        cartInfo.TotalPrice = cartInfo.Quantity * cartInfo.Price;
                        cartInfo.OrderNumber = ordernumber;
                        cartInfo.CartStatus = 2;

                        cart.Update(cartInfo);

                        orderprice += cartInfo.TotalPrice.Value;
                    }
                }
            }

            orderInfo.UserGUID = userid;
            orderInfo.OrderNumber = ordernumber;
            orderInfo.OrderStatus = 0;
            orderInfo.TotalPrice = orderprice;

            int orderid = order.Add(orderInfo);

            if (userid != "")
            {
                Response.Redirect("/Product/ReturnCustomer.aspx?OrderID=" + Server.UrlEncode(Utility.Encryption.Encrypt(orderid.ToString())));
                //Response.Redirect("/Customer/signin.aspx?next=" + Server.UrlEncode("/goods/CartNext.aspx?tempuserid=" + userid + "&orderid=" + Utility.Encryption.Encrypt(orderid.ToString())));
            }
            else
            {
                Response.Redirect("/Product/CheckOut.aspx?OrderID=" + Server.UrlEncode(Utility.Encryption.Encrypt(orderid.ToString())));
            }
        }
    }

}